package com.cqan.httpclient;

import java.util.Map;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;

public class HttpRequestClient {

	private static final HttpClient client = new HttpClient();
	private WindowMain windowMain;

	public HttpRequestClient(WindowMain windowMain) {
		this.windowMain = windowMain;
	}

	public void post(String uri, Map<String, String> params, String content,
			Map<String, String> headers) {
		windowMain.setResult("", false);
		try {
			PostMethod post = new PostMethod(uri);
			client.executeMethod(post);
			windowMain.setResult(post.getStatusLine().toString(), false);
			Header[] header = post.getResponseHeaders();
			for (Header h : header) {
				windowMain.setResult(h.getName() + ":" + h.getValue(), true);
			}
			windowMain.setResult("\n", true);
			windowMain.setResult(post.getResponseBodyAsString(), true);
		} catch (Exception e) {
			windowMain.setResult(e.getMessage(), false);
		}
	}

	public void get(String uri, Map<String, String> params, String content,
			Map<String, String> headers) {

		try {
			GetMethod get = new GetMethod(uri);
			client.executeMethod(get);
			windowMain.setResult(get.getStatusLine().toString(), false);
			Header[] header = get.getResponseHeaders();
			for (Header h : header) {
				windowMain.setResult(h.getName() + ":" + h.getValue(), true);
			}
			windowMain.setResult("\n", true);
			windowMain.setResult(get.getResponseBodyAsString(), true);
		} catch (Exception e) {
			windowMain.setResult(e.getMessage(), false);
		}
	}

	public void put(String uri, Map<String, String> params, String content,
			Map<String, String> headers) {

		try {
			PutMethod get = new PutMethod(uri);
			client.executeMethod(get);
			windowMain.setResult(get.getStatusLine().toString(), false);
			Header[] header = get.getResponseHeaders();
			for (Header h : header) {
				windowMain.setResult(h.getName() + ":" + h.getValue(), true);
			}
			windowMain.setResult("\n", true);
			windowMain.setResult(get.getResponseBodyAsString(), true);
		} catch (Exception e) {
			windowMain.setResult(e.getMessage(), false);
		}
	}

	public void delete(String uri, Map<String, String> params, String content,
			Map<String, String> headers) {

		try {
			DeleteMethod get = new DeleteMethod(uri);
			client.executeMethod(get);
			windowMain.setResult(get.getStatusLine().toString(), false);
			Header[] header = get.getResponseHeaders();
			for (Header h : header) {
				windowMain.setResult(h.getName() + ":" + h.getValue(), true);
			}
			windowMain.setResult("\n", true);
			windowMain.setResult(get.getResponseBodyAsString(), true);
		} catch (Exception e) {
			windowMain.setResult(e.getMessage(), false);
		}
	}
}
